package com.web.expspringboot.service.hustImpl;

import com.web.expspringboot.dto.Contact;
import com.web.expspringboot.entity.ProductSt;
import com.web.expspringboot.mapper.MessageMapper;
import com.web.expspringboot.mapper.PurchaseMapper;
import com.web.expspringboot.service.PurchaseService;
import com.web.expspringboot.service.ProductService;
import com.web.expspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Profile("hust")
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    @Lazy
    private ProductService productService;

    @Autowired
    UserService userService;

    @Override
    public int addNewPurchase(long productId, long customerId) {
        ProductSt product = productService.getProductById(productId);
        if (product.getOwnerId() == customerId) return -3;
        if (product.getStatus() > 1) return -2;
        if (purchaseMapper.addNewPurchase(productId, product.getOwnerId(), customerId)) return 0;
        return -1;
    }

    @Override
    public List<ProductSt> getPurchaseProducts() {
        long userId = userService.getSessionUserId();
        return purchaseMapper.getPurchaseProducts(userId);
    }

    @Override
    public boolean deletePurchase(long productId) {
        return purchaseMapper.deletePurchase(productId);
    }

    @Override
    public Contact getContact(long productId) {
        return purchaseMapper.getContact(productId);
    }
}
